%% This file was generator by proto_to_erl.py. DO NOT MODIFY MANNUAL.

-module(icloud_protocol).

-export([encode/2,encode/4,decode/2,decode/1]).

-include("message_type.hrl").
-include("protocol.hrl").

-import(protocol_util, [decode_list/2, encode_list/2, 
                        encode_string/2, decode_string/1, decode_float/1]).

encode(Type, Uid, TimeStampMs, Data) ->
    <<Type:?UINT16, Uid:?UINT32, TimeStampMs:?UINT64,
     (serialize(encode, Type, Data))/binary>>.
encode(Type, Data) when is_atom(Type) ->
    serialize(encode, Type, Data);
encode(Type, Data) ->
    encode(Type, 0, 0, Data).

decode(Bin) ->
    <<MsgType:?UINT16, Uid:?UINT32, TimeStampMs:?UINT64,
        Bin1/binary>> = Bin,
    {Data, Bin2} = decode(MsgType, Bin1) ,
    {MsgType, {Uid, TimeStampMs}, Data, Bin2}.
decode(Type, Bin) ->
    serialize(decode, Type, Bin).
    


%%% decode CHAR_BASIC 
serialize(decode, 'CHAR_BASIC', BinData0) ->
    <<Char_oid:?UINT32, Account_oid:?UINT32, Create_entryway:?UINT32, Char_order:?INT32, Gm_authority:?INT16, BinData1/binary>> = BinData0,
    {Name, BinData2} = decode('STRING', BinData1),
    {Title_id, BinData3} = decode_list(fun(__VAR__) -> <<Val:?INT64, Remain/binary>>=__VAR__, {Val, Remain} end, BinData2),
    {{Char_oid, Account_oid, Create_entryway, Char_order, Gm_authority, Name, Title_id}, BinData3};
%%% encode CHAR_BASIC
serialize(encode, 'CHAR_BASIC', {Char_oid, Account_oid, Create_entryway, Char_order, Gm_authority, Name, Title_id}) ->
    <<Char_oid:?UINT32, 
    Account_oid:?UINT32, 
    Create_entryway:?UINT32, 
    Char_order:?INT32, 
    Gm_authority:?INT16, 
    (encode_string(Name, length(Name)))/binary, 
    (encode_list(fun(__VAR__) -> <<__VAR__:?INT64>> end, Title_id))/binary>>;

%%% decode CHAR_BASIC2 
serialize(decode, 'CHAR_BASIC2', BinData0) ->
    <<Char_oid:?UINT32, Account_oid:?UINT32, Create_entryway:?UINT32, Char_order:?INT32, Gm_authority:?INT16, BinData1/binary>> = BinData0,
    {Name, BinData2} = decode('STRING', BinData1),
    {Title_id, BinData3} = decode_list(fun(__VAR__) -> <<Val:?INT64, Remain/binary>>=__VAR__, {Val, Remain} end, BinData2),
    {{Char_oid, Account_oid, Create_entryway, Char_order, Gm_authority, Name, Title_id}, BinData3};
%%% encode CHAR_BASIC2
serialize(encode, 'CHAR_BASIC2', {Char_oid, Account_oid, Create_entryway, Char_order, Gm_authority, Name, Title_id}) ->
    <<Char_oid:?UINT32, 
    Account_oid:?UINT32, 
    Create_entryway:?UINT32, 
    Char_order:?INT32, 
    Gm_authority:?INT16, 
    (encode_string(Name, length(Name)))/binary, 
    (encode_list(fun(__VAR__) -> <<__VAR__:?INT64>> end, Title_id))/binary>>;

%%% decode NotifyCharBasicMessage 
serialize(decode, ?MSG_NOTIFY_CHAR_BASIC, BinData0) ->
    {Char_basic, BinData1} = decode('CHAR_BASIC', BinData0),
    {Other_basic, BinData2} = decode_list(fun(__VAR__)-> decode('CHAR_BASIC', __VAR__) end, BinData1),
    {{Char_basic, Other_basic}, BinData2};
%%% encode NotifyCharBasicMessage
serialize(encode, ?MSG_NOTIFY_CHAR_BASIC, {Char_basic, Other_basic}) ->
    <<(serialize(encode, 'CHAR_BASIC', Char_basic))/binary, 
    (encode_list(fun(__VAR__) -> encode('CHAR_BASIC', __VAR__) end, Other_basic))/binary>>;

serialize(decode, 'STRING', DATA) ->
    decode_string(DATA);
serialize(decode, 'FLOAT', DATA) ->
    decode_float(DATA);
serialize(encode, Type, Data) ->
    throw({encode_protocol_fail, Type, Data});
serialize(decode, Type, Data) ->
    throw({encode_protocol_fail, Type, Data}).
    
